import {ref} from 'vue'
import useInstance from '@/hooks/useInstance'
import {bannerSave,bannerDelete,bannerEdit} from '@/api/banner';
export const useBanner = (loadList)=>{
  const {global} = useInstance()
  const addBannerRef = ref();

  const addBtn = ()=>{
    console.log('------------添加Banner-----------',addBannerRef.value);
    addBannerRef.value?.show('add')
  }

  const editBtn = (index,row)=>{
    console.log('------------编辑Banner-----------',row);
    addBannerRef.value?.show('edit',row)
  }
   //删除
   const deleteBtn = async(index,row) =>{
        console.log('-------deleteBtn---------',row);
        const res = await bannerDelete(row.bannerId)
        if(res.code === 200){
          global.$message({ message: res.msg, type: 'success' })  
        }
        // 删除完成再次查询数据
        loadList()
   }
   //保存
   const save = async(addBannerModel) =>{
    let  res;
    if(addBannerModel.type == 'add'){
     res = await bannerSave(addBannerModel)
    }else{
      res = await bannerEdit(addBannerModel)
    }
    if(res.code == 200){
      loadList()
      global.$message({ message: res.msg, type: 'success' })         
    }
     
   }
   return {addBannerRef,addBtn,editBtn,deleteBtn,save}
}